/**
 * 联系人角色维护
 * @author miaoyl
 * @date 2018/01/11
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type:'checkbox',fixed: 'left'}
            , {field: 'GWAW001', width: 160, title: '角色编号', fixed: 'left',sort: true,search:'default'}
            , {field: 'GWAWL003', width: 160, title: '说明'}
            ,{field: 'GWAWL004', width: 160, title: '助记码'}
            , {field: 'GWAWACTI', width: 160, title: '有效否',filterDatas: app.datas_('sys_y_n')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai243setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai243SettingAction,
        editArea = ['600px', '360px']
    ;

    var Gwai243 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai243',
                _searchs: [{
                    id: 'gwai243', tableId: 'gwai243_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwai243_edit', tpl: 'gwai243_edit'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai243_table'),
                    url: g_ctxPath + 'crm/gwai243/getGwawlist',
                    exportsUrl: g_ctxPath + 'crm/gwai243/getGwawlist',
                    exportsName: '联系人角色数据',
                    title: '联系人角色信息表',
                    method: 'post',
                    id: 'gwai243_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai243setting-action').innerHTML).render({}, function (string) {
                    gwai243SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai243_table)', function (obj) {

                    layer.tips(gwai243SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwai243_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: '联系人角色信息编辑',
                                                area:editArea
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwai243_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai243_edit',
                        tagData: {
                            obj: obj,
                            is_detail: true
                        },
                        index: 0,
                        openOpts: {
                            title: '联系人角色信息编辑',
                            area:editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai243_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai243_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '联系人角色信息编辑',
                                    area:editArea
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai243_table', where: table_param});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai243/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '联系人角色信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwai243_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai243');
                    _temp.GWAWENT = v.GWAWENT;
                    _temp.GWAW001 = v.GWAW001;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai243/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    okmsg: '联系人角色信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai243',
                    methods: {}
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai243();

});